<template>
    <!-- 商品分类导航 -->
    <div class="type-nav">
        <div class="container-all">
            <div class="all-title" @mouseleave="leaveShow" @mouseenter="enterShow">
                <h2 class="all">全部商品分类</h2>
                <el-link v-for="item in navItems" :key="item" type="primary">
                    <span>{{ item }}</span>
                </el-link>
                <div class="sort" v-show="isShowSort">
                    <div class="all-sort-list2" @click="goSearch">

                        <!-- item为一级级分类 -->
                        <div class="item" v-for="c1 in categoryList.slice(0, 15)" :key="c1.categoryId">
                            <h3>
                                <!--可点击的一级分类 -->
                                <a data-level="1" :data-categoryName="c1.categoryName"
                                    :data-category1Id="c1.categoryId">
                                    {{ c1.categoryName }}</a>
                            </h3>
                            <div class="item-list clearfix">
                                <!-- 每一个subitem就是一个二级分类-->
                                <div class="subitem" v-for="c2 in c1.categoryChild" :key="c2.categoryId">
                                    <dl class="fore">
                                        <dt>
                                            <!-- 可点击的二级级分类 -->
                                            <a data-level="2" :data-categoryName="c2.categoryName"
                                                :data-category2Id="c2.categoryId">{{ c2.categoryName }}</a>
                                        </dt>
                                        <dd>
                                            <!-- 每一个em就是一个三级分类  -->
                                            <em v-for="c3 in c2.categoryChild" :key="c3.categoryId">
                                                <a data-level="3" :data-categoryName="c3.categoryName"
                                                    :data-category3Id="c3.categoryId">{{ c3.categoryName }}</a>
                                            </em>
                                        </dd>
                                    </dl>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>

</template>

<script>
// import { getCategoryList } from '@/api/api';
import { mapState } from 'vuex';
export default {
    name: 'TypeNav',
    data() {
        return {
            navItems: [
                '服装城',
                '美妆馆',
                '逛超市',
                '全球购',
                '闪购',
                '团购',
                '有趣',
                '秒杀'
            ],
            // categoryList: [],
            isShowSort: true, // 是否显示排序
        }
    },
    mounted() {
        this.$store.dispatch('setCategoryList');// 获取分类列表
        //当组件挂载成功，让isShowSort为false
        if (this.$route.path != '/home') {
            this.isShowSort = false;
        }
    },
    computed: {
        ...mapState({
            categoryList: state => state.home.categoryList,
        })
    },
    methods: {
        goSearch(event) {
            let element = event.target; // 获取点击的元素
            //dataset属性返回元素的自定义属性,获取元素的data-属性
            let { categoryname, category1id, category2id, category3id } = element.dataset;

            // 确保存在分类名称
            if (categoryname) {
                let location = { name: 'search' };
                let query = { categoryName: categoryname };

                if (category1id) {
                    query.category1Id = category1id;  // 一级分类
                } else if (category2id) {
                    query.category2Id = category2id;  // 二级分类
                } else if (category3id) {
                    query.category3Id = category3id;  // 三级分类
                }
                // // 路由跳转之前判断参数类型并合并
                if (this.$route.params) {
                    location.params = this.$route.params;
                    location.query = query;
                    this.$router.push(location);
                }

            }
        },
        //当鼠标移入时，商品分类进行展示;
        enterShow() {
            this.isShowSort = true;
        },
        //当鼠标移出时，商品分类隐藏;
        leaveShow() {
            if (this.$route.path !== '/home') {
                this.isShowSort = false;
            }
        }

    }
}
</script>

<style scoped>
.type-nav {
    border-bottom: 2px solid #e1251b;
}

.el-link {
    height: 45px;
    margin: 0 22px;
    line-height: 45px;
    font-size: 18px;
    color: #333;
    text-decoration: none;
}

.el-link:hover {
    color: #e1251b;
}

.all-title {
    display: flex;
    justify-content: space-between;
    align-items: center;
}


.container-all {
    width: 1200px;
    margin: 0 auto;
    display: flex;
    position: relative;
}

h2 {
    width: 210px;
    height: 45px;
    background-color: #e1251b;
    line-height: 45px;
    text-align: center;
    color: #fff;
    font-size: 18px;
    font-weight: bold;
}

.sort {
    left: 0;
    top: 45px;
    width: 210px;
    height: 461px;
    position: absolute;
    background: #eaeaea;
    z-index: 999;
}

.item {
    width: 210px;
    cursor: pointer;
    /* position: relative; */
}

.item h3 {
    line-height: 30px;
    font-size: 14px;
    font-weight: 400;
    overflow: hidden;
    padding: 0 20px;
}

.item h3 a {
    display: block;
    /* width: 210px; */
    /* height: 32px; */
    color: #333;
    text-decoration: none;
}

.item:hover {
    background-color: #44bedf;
}

.item h3 a:hover {
    color: #fff;
}

.item-list {
    display: none;
    position: absolute;
    width: 734px;
    min-height: 460px;
    background: #f7f7f7;
    left: 210px;
    border: 1px solid #ddd;
    top: 0;
    z-index: 9999 !important;
}

.item-list .subitem {
    float: left;
    width: 650px;
    padding: 0 4px 0 8px;
}

.item-list .subitem dl {
    border-top: 1px solid #eee;
    padding: 6px 0;
    overflow: hidden;
    zoom: 1;
}

.item-list .subitem dl.fore {
    border-top: 0;
}

.item-list .subitem dl dt {
    float: left;
    width: 54px;
    line-height: 22px;
    text-align: right;
    padding: 3px 6px 0 0;
    font-weight: 700;
}

.item-list .subitem dl dd {
    float: left;
    width: 415px;
    padding: 3px 0 0;
    overflow: hidden;
}

.item-list .subitem dl dd em {
    float: left;
    height: 14px;
    line-height: 14px;
    padding: 0 8px;
    margin-top: 5px;
    border-left: 1px solid #ccc;
}

.item-list a:hover {
    color: #3fbbde;
}

.item:hover .item-list {
    display: block;

}
</style>